﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bll;
using model;
using System.Web.Security;

public partial class Master : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session["username"] == null)
            {
                lblWelcome.Text = "欢迎您！";
                lblMess.Text = null;
                btnQuit.Visible = false;
                LoginTable.Visible = true;
                InfoTable.Visible = false;
                btnLogin.Visible = true;
                btnReg.Visible = true;
                admintable.Visible = false;
            }
            else
            {
                string str = "欢迎您，" + Session["username"].ToString() + "！";
                lblWelcome.Text = str;
                lblMess.Text = Session["username"].ToString() + ",欢迎登录";
                btnQuit.Visible = true;
                LoginTable.Visible = false;
                InfoTable.Visible = true;
                btnLogin.Visible = false;
                btnReg.Visible = false;
                admintable.Visible = false;
                if (Session["usergroup"].ToString() == "student")
                {
                    lblInfo.Text = "简历管理";
                    lblSch.Text = "招聘搜索";
                    lblGoto.Visible = true;
                    
                }
                if (Session["usergroup"].ToString() == "enterprise")
                {
                    lblInfo.Text = "招聘管理";
                    lblSch.Text = "简历搜索";
                    lblGoto.Visible = false;
                    //liGoto.Visible = false;
                }
                if(Session["usergroup"].ToString()=="admin")
                {
                    InfoTable.Visible = false;
                    admintable.Visible = true;
                }
            }
        }
    }

    protected void btnQuit_Click(object sender, EventArgs e)
    {
        Session["username"] = null;
        Session["usergroup"] = null;
        Session["userid"] = null;
        Response.Write("<script>alert('退出成功！');location.href='Default.aspx';</script>");
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        if (rs.Checked == true)
        {
            Response.Redirect("Registers.aspx");
        }
        else
        {
            Response.Redirect("Registere.aspx");
        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (rs.Checked == true)
        {
            StudentBll bll = new StudentBll();
            string sno = txtId.Text;
            string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text,"MD5");
            try
            {
                int stat = bll.Login(sno, pwd);
                if (stat == 1)
                {
                    if (bll.getisok(sno) == 0)
                    {
                        Response.Write("<script>alert('您的帐号已被冻结,请联系管理员！');history.back();</script>");
                        return;
                    }
                    Session["userid"] = sno;
                    Session["usergroup"] = "student";
                    Session["username"] = bll.Login(sno);
                    Response.Write("<script>alert('登录成功！');location.href='Default.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('用户名或密码错误！');history.back();</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        else
        {
            EnterpriseBll bll = new EnterpriseBll();
            string eno = txtId.Text;
            string epwd = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text,"MD5");
            try
            {
                int stat = bll.Login(eno, epwd);
                if (stat == 1)
                {
                    if (bll.getisok(eno) == 0)
                    {
                        Response.Write("<script>alert('您的帐号已被冻结,请联系管理员！');history.back();</script>");
                        return;
                    }
                    Session["userid"] = eno;
                    Session["usergroup"] = "enterprise";
                    Session["username"] = bll.Login(eno);
                    Response.Write("<script>alert('登录成功！');location.href='Default.aspx';</script>");
                }
                else
                {
                    Response.Write("<script>alert('用户名或密码错误！');history.back();</script>");
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}
